import course from 'api/course.js'
import { arrayCopy, swap } from 'utils'



export default {
	namespaced: true,

	state: {
		courseSelection: -1, 
		examTypeSelection: -1,
		problemNumberSelection: -1,
		questypeSelections: [],
		
		questypes: [],
		problemNumbers: [25, 50, 100],
		
		term: 0,
	},
	mutations: {
		questypes({ questypes }, newQuestypes) {
			arrayCopy(questypes, newQuestypes.map(ele => {
				[ele.key, ele.value] = [ele.value, ele.key];
				return ele;
			}));
		},
		courseSelection(state, value){
			state.courseSelection = value;
		},
		examTypeSelection(state, value){
			state.examTypeSelection = value;
		},
		problemNumberSelection(state, value){
			state.problemNumberSelection = value;
		},
		questypeSelections(state, value){
			arrayCopy(
				state.questypeSelections,
				value
			);
		},

		selectCourse(state, value){
    		state.courseSelection = value;
    		state.term++;
    	},
    	selectExamType(state, value){
    		state.examTypeSelection = value;
    		state.term++;
    	},
    	selectProblemNumber(state, value){
    		state.problemNumberSelection = value;
    		state.term++;
    	},
    	selectQuestype(state, value){
    		state.term++;
    	},
		clear(state){
			state.courseSelection = -1; 
			state.examSelection = -1;
			state.questypeSelections = [];
			state.problemNumberSelection = -1;
			state.term = 0;
		},
	},
	actions: {

		getQuestypes({ commit }) {
			return course.getQuestypes()
							.done(data => {
								commit('questypes', data);
							});
		},

		init({ dispatch}){
			dispatch('getQuestypes');
		}
	}
}